package com.china08.yxyapi.api.home.activity.weike;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResBook;
import com.china08.yxyapi.entity.db.res.YxyResSemester;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.repository.db.YxyResBookRepository;
import com.china08.yxyapi.repository.db.YxyResSemesterRepository;
import com.china08.yxyapi.util.BookIdUtil;

import io.swagger.annotations.ApiOperation;

@CrossOrigin
@RestController
public class ActivitySemesterController extends YxyApiBaseController {

    @Autowired
    private YxyResBookRepository yrbr;

    @Autowired
    private YxyResSemesterRepository yxyResSemesterRepository;

    @ApiOperation(value = "获得学期")
    @RequestMapping(value = "/api_yw/home/activity/tea/semester", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public List<RespModel> find(@RequestBody ReqModel reqModel) {
        List<RespModel> list = new ArrayList<ActivitySemesterController.RespModel>();

        List<String> list2 = BookIdUtil.getBookId(reqModel.phase.trim());

        List<YxyResBook> list3 = yrbr.findByIdInAndPhaseIdAndSubjectIdAndVersionId(list2, reqModel.phase,
                reqModel.subject, reqModel.version);

        List<String> semesters = yrbr.findSemesters(reqModel.phase, reqModel.subject, list2, reqModel.version);

        if (semesters.size() > 0) {
            List<YxyResSemester> yxyResSemesters = yxyResSemesterRepository
                    .findByIdInAndStatusOrderByOrderNum(semesters, 1);

            for (YxyResSemester yxyResSemester : yxyResSemesters) {
                YxyResBook yxyResType = list3.stream()
                        .filter(x -> StringUtils.equals(x.getSemersterId(), yxyResSemester.getId())).findFirst().get();
                if (yxyResType != null) {
                    RespModel model = new RespModel();
                    model.id = yxyResType.getSemersterId();
                    model.name = yxyResType.getSemersterName();
                    model.bookId = yxyResType.getId();
                    list.add(model);
                }
            }
        }
        return list;
    }

    protected static class ReqModel extends YxyApiReqModel {
        public String subject;
        public String version;
        public String phase;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(subject, null), "请指定学科");
            Assert.notNull(StringUtils.defaultIfBlank(version, null), "请指定版本");
            Assert.notNull(StringUtils.defaultIfBlank(phase, null), "请指定学段");
        }

    }

    protected static class RespModel {
        public String id;
        public String name;
        public String bookId;
    }

}
